//
//  LoanProductViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/26.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class LoanProductViewController: KMViewController {
    
    fileprivate let cellId = "loanProductCell"
    fileprivate var dataArray = [LoanRecordListModel]()
    
    fileprivate var tableView : UITableView!
    
    var loanDetailsBlock : ((String)->Void)? //查看贷款详情
    
    init(array:[LoanRecordListModel]) {
        super.init(nibName: nil, bundle: nil)
        
        self.dataArray = array
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        createTableView()
    }
    
    //MARK: 创建tableView
    fileprivate func createTableView() {
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(LoanProductCell.self, forCellReuseIdentifier: cellId)
        self.view.addSubview(tableView)
        
        let headView = ProdouctDetailsHeadView.init(frame: .init(x: 0, y: 0, width: ScreenW-RATIO(maxNum: 40), height: RATIO_H(maxNum: 50)))
        tableView.tableHeaderView = headView
        
        let footView = UIButton.init(frame: .init(x: 0, y: 0, width: ScreenW-RATIO(maxNum: 40), height: RATIO_H(maxNum: 50)))
        footView.setTitle(close, for: .normal)
        footView.setTitleColor(UIColor.white, for: .normal)
        footView.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        footView.addTarget(self, action: #selector(closeDetails), for: .touchUpInside)
        tableView.tableFooterView = footView
        
        tableView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.equalTo(CGFloat(dataArray.count) * RATIO_H(maxNum: 40) + RATIO_H(maxNum: 100))
        }
    }
    
    //MARK: 关闭页面
    @objc fileprivate func closeDetails() {
        
        self.dismiss(animated: false, completion: nil)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.dismiss(animated: false, completion: nil)
    }
    

}

extension LoanProductViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 40)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let model = dataArray[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? LoanProductCell
        cell?.selectionStyle = .none
        cell?.titleLabel.text = model.organization_name! + "-" + model.product_name!
        if "\(model.selected ?? "")" == "0" {
            cell?.selectLabel.text = yes_title
        }else{
            cell?.selectLabel.text = no_title
        }
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.loanDetailsBlock!(dataArray[indexPath.row].id!)
        self.dismiss(animated: false, completion: nil)
    }
}
